package com.uenpay.im.ui.old.activity.company;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.uenpay.im.IMApplication;
import com.uenpay.im.R;
import com.uenpay.im.ui.base.BaseActivity;

import java.util.ArrayList;


/**
 * @author SL
 * @created 2016-3-19 下午3:22:46
 * @copyRight http://www.qiyunxin.com
 * @function 只有某个公司所有部门列表
 */
public class OnlyDepartmentListActivity extends BaseActivity {

	private View loading;
	private int entid;
	/**
	 * 是否为单选
	 */
	private boolean isRadio = false;
	private CompanyHandle mCompanyHandle = new CompanyHandle();
	private DepartmentsAdapter mDepartmentsAdapter = null;
	private ListView listview;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_list_layout);
		IMApplication.getInstance().addActivity(this);
		entid = getIntent().getIntExtra("entid", 0);
		isRadio = getIntent().getBooleanExtra("isRadio", false);
		initView();
		initListener();
		getData();
	}

	@Override
	protected void initView() {
		((TextView) findViewById(R.id.title_textview))
				.setText(R.string.department);
		loading = findViewById(R.id.loading);
		listview = (ListView) findViewById(R.id.listview);

	}

	@Override
	protected void initListener() {

		findViewById(R.id.back_layout).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						finish();
					}
				});
		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				DepartmentEntity departmentEntity = (DepartmentEntity) arg0
						.getAdapter().getItem(arg2);
				if (departmentEntity != null) {
					Intent intent = new Intent(OnlyDepartmentListActivity.this,
							ChooseEmployeeActivity.class);
//					Intent intent = new Intent(OnlyDepartmentListActivity.this,
//							OnlyEmployeeListActivity.class);
					intent.putExtra("departmentEntity", departmentEntity);
					intent.putExtra("isRadio", isRadio);
					startActivityForResult(intent, 100);
				}
			}
		});

	}

	private void getData() {
		loading.setVisibility(View.VISIBLE);
		mCompanyHandle.getDepartByPidList(0, entid, new CompanyHandle.IGetDepartListResult() {

			@Override
			public void onGetDepartResult(int status, String msg,
					ArrayList<DepartmentEntity> arrayList) {
				loading.setVisibility(View.GONE);
				mDepartmentsAdapter = new DepartmentsAdapter(
						OnlyDepartmentListActivity.this, arrayList, false);
				listview.setAdapter(mDepartmentsAdapter);
			}
		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == 100 && resultCode == RESULT_OK) {
			// TODO
			if(data != null){
				ArrayList<EmployeeEntity> arraylist = (ArrayList<EmployeeEntity>) data.getSerializableExtra("arraylist");
//				EmployeeEntity mEmployeeEntity = (EmployeeEntity) data.getSerializableExtra("mEmployeeEntity");
				Intent intent = new Intent();
				intent.putExtra("arraylist", arraylist);
				setResult(RESULT_OK, intent);
				OnlyDepartmentListActivity.this.finish();
			}
		}
	}

	@Override
	public void onBackPressed() {
		finish();
		super.onBackPressed();
	}

	@Override
	protected void onDestroy() {
		IMApplication.getInstance().removeActivity(this);
		super.onDestroy();
	}

}
